package com.amado.course41Annotation;

import java.lang.reflect.Field;

/**
 * Created by Administrator on 2018/8/4.
 */
public class S03AnnotataionValue {
	
    public static String checkAnnotationValid(Object obj) {
        if(obj==null){
            return null;
        }
        //获取所有的字段包含private
        Field[] fields = obj.getClass().getDeclaredFields();
        //只能获取public修饰的字段
        obj.getClass().getFields();
        StringBuilder stringBuilder = new StringBuilder();
        for (Field field : fields) {
            //设置可访问，如果是私用的必须设置此参数才能访问
            field.setAccessible(true);
            //拿到字段上的注解
            S02ValidStringFeild validStringFeild = field.getAnnotation(S02ValidStringFeild.class);
            //是否有这个注解
            if (validStringFeild == null) {
                continue;
            }
            //字段类型
            Class type = field.getType();
            Object fieldValue = null;
            try {
            	//obj.getName();  张三
            	//name->obj  张三
            	//字段对应的值
                fieldValue = field.get(obj);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            //判断自定义注解的length!=-1
            if (validStringFeild.length() != -1) {
                if (fieldValue == null) {
                	stringBuilder.append(field.getName()).append(" ").append(validStringFeild.desc()).append("为空").append("\n");
                    continue;
                }
            }
            if (type.equals(String.class)) {
                String fieldStringValue = (String) fieldValue;
                //判断长度,fieldStringValue.length():字段所值，  validStringFeild.length()注解上配置的值
                if (validStringFeild.length() != -1 && fieldStringValue.length() != validStringFeild.length()) {
                    stringBuilder.append(field.getName()).append(" ").append(validStringFeild.desc()).append("长度不等于").append(validStringFeild.length()).append("\n");
                    continue;
                }
                //判断最大长度,fieldStringValue.length():字段所值，  validStringFeild.length()注解上配置的值
                if (validStringFeild.maxLength() != -1 && fieldStringValue.length() > validStringFeild.maxLength()) {
                	stringBuilder.append(field.getName()).append(" ").append(validStringFeild.desc()).append("长度超出").append(validStringFeild.maxLength()).append("\n");
                }
            }else if (type.equals(Integer.class)) {
                Integer fieldIntegerValue = (Integer) fieldValue;
                //判断最大值,fieldStringValue.length():字段所值，  validStringFeild.length()注解上配置的值
                if (fieldIntegerValue != null && validStringFeild.maxValue() != -1 && fieldIntegerValue > validStringFeild.maxValue()) {
                    System.out.println(field.getName()+" "+validStringFeild.desc() + "超出" + validStringFeild.maxValue());
                    stringBuilder.append(field.getName()).append(" ").append(validStringFeild.desc()).append("超出").append(validStringFeild.maxValue()).append("\n");
                }
            }
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        Student student = new Student("张三", "185755883961", 200);
        checkAnnotationValid(student);
    }
}
